/**************************************************************************************
* MapPanel.java
* 
*************************************************************************************/
package creid.mythos.ui;

import java.awt.Graphics;

import creid.mythos.shared.SpriteMap;

public class MapPanel extends GamePanel
{

	//Constants/////////////////////////////////////////////////////////////////////////

	private static final long serialVersionUID = 7724145874753627530L;
	
	//Desired window size
	static final int WIDTH = SpriteMap.MAP_WIDTH * GameUI.FONT_HORIZONTAL_SPACING;
	static final int HEIGHT = SpriteMap.MAP_HEIGHT * GameUI.FONT_VERTICAL_SPACING;
	static final int COLOR_INDEX = 1;
	
	//Attributes////////////////////////////////////////////////////////////////////////
	
	//Sprites to draw
	private SpriteMap map;

	//Constructors//////////////////////////////////////////////////////////////////////
	
	public MapPanel()
	{
		setMap(null);
		setSize(WIDTH, HEIGHT);
	}

	//Methods///////////////////////////////////////////////////////////////////////////

	void setMap(SpriteMap map)
	{
		this.map = map;
	}

	@Override
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		//Only draw a map if we need to
		if (map != null)
		{
			int drawX = 1;
			int drawY = 1;
			
			for (int x = 0; x < SpriteMap.MAP_WIDTH; x++)
			{
				drawX += GameUI.FONT_HORIZONTAL_SPACING;
				drawY = 0;
				
				for (int y = 0; y < SpriteMap.MAP_HEIGHT; y++)
				{
					drawY += GameUI.FONT_VERTICAL_SPACING;

					//Draw a red box on the target
					if (x == map.getTargetX() && y == map.getTargetY())
					{
						g.setColor(GameUI.colors.get(ColorData.RED));
						g.drawRoundRect(drawX, drawY - GameUI.FONT_VERTICAL_SPACING + 2, 
								GameUI.FONT_HORIZONTAL_SPACING, GameUI.FONT_VERTICAL_SPACING + 1, 
								GameUI.FONT_HORIZONTAL_SPACING/4, GameUI.FONT_VERTICAL_SPACING/4);
					}
					
					//Draw a yellow box over highlit square
					if (x == map.getHighlightX() && y == map.getHighlightY())
					{
						g.setColor(GameUI.colors.get(ColorData.YELLOW));
						g.drawRoundRect(drawX, drawY - GameUI.FONT_VERTICAL_SPACING + 2, 
								GameUI.FONT_HORIZONTAL_SPACING, GameUI.FONT_VERTICAL_SPACING + 1, 
								GameUI.FONT_HORIZONTAL_SPACING/4, GameUI.FONT_VERTICAL_SPACING/4);						
					}
					
					char[] s = map.getSprite(x, y);
					
					if(map.isSeen(x, y))
						g.setColor(GameUI.colors.get(s[COLOR_INDEX]));
					else
						g.setColor(GameUI.colors.get(ColorData.DARK_GRAY));
					
					g.drawChars(s, 0, 1, drawX, drawY);
				}
			}
		}
	}

}
